package com.appclient;

import calculations.PassCost;
import com.table.CarTableController;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.ParkingspacesModel;
import com.carpark.ejb.Model.PassesrecordsModel;
import com.carpark.ejb.Model.ReceivablesModel;
import com.carpark.ejb.Model.TarrifsModel;
import com.table.ClientTableController;
import com.wizardClient.ChooseAccountingController;
import com.wizardClient.ChooseCarController;
import com.wizardClient.ChooseClientController;
import com.wizardClient.ChooseParkingSpaceController;
import com.wizardClient.ChooseTarrifController;
import com.wizardClient.ClientScreensFramework;
import com.wizardClient.EndPageController;
import java.io.IOException;
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import popup.PopupController;

/**
 *
 * @author Sawior
 */
public class ClientWizardController implements Initializable, ControlledScreen {

    private ScreensController myController;
    private ClientScreensFramework screensFramework;
    private ClientsModel client;
    private CarsModel car;
    private AccountingdataModel accounting;
    private TarrifsModel tarrif;
    private ParkingspacesModel space;
    private Integer selectedClient;
    private Integer selectedCar;
    ParkingpassesModel passDB;
    private Date startDate;
    private Date endDate;
    @FXML
    private StackPane wizardPage;
    @FXML
    private Button next;
    @FXML
    private Button prev;
    @FXML
    private Button find;
    @FXML
    private Button useDefault;
    @FXML
    private Label step1;
    @FXML
    private Label step2;
    @FXML
    private Label step3;
    @FXML
    private Label step4;
    @FXML
    private Label step5;
    @FXML
    private Label step6;
    private boolean isConfirmed = false;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        screensFramework = new ClientScreensFramework(this);
        wizardPage.getChildren().addAll(screensFramework);
        step1.setTextFill(Color.GREEN);
    }

    @Override
    public void setScreenParent(ScreensController screenParent) {
        myController = screenParent;
    }

    @FXML
    private void handleButtonAction(ActionEvent event) {
        String actual = screensFramework.getScreensController().getActual();
        if (event.getSource() == next) {
            chooseNextPage();
        }
        if (event.getSource() == prev) {
            choosePrevPage();
        }
        if (event.getSource() == find) {
            if (actual.equals(ClientScreensFramework.FIRST_SCREEN)) {
                showClientTableDialog();
                if (this.selectedClient != null) {
                    ((ChooseClientController) screensFramework.getScreensController().getController(ClientScreensFramework.FIRST_SCREEN)).loadData(selectedClient);
                }
            }
            if (actual.equals(ClientScreensFramework.SECOND_SCREEN)) {
                showCarTableDialog();
                if (this.selectedCar != null) {
                    ((ChooseCarController) screensFramework.getScreensController().getController(ClientScreensFramework.SECOND_SCREEN)).loadData(selectedCar);
                }
            }
        }
    }

    public boolean showClientTableDialog() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("clientTable.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Wybierz osobę");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            ClientTableController controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setWizard(this);

            dialogStage.showAndWait();

            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean showCarTableDialog() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("carTable.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Wybierz pojazd");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            CarTableController controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setWizard(this);

            dialogStage.showAndWait();

            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    @FXML
    private void loadDefaultDate(ActionEvent event) {
        String actual = screensFramework.getScreensController().getActual();
        if (actual.equals(ClientScreensFramework.THIRD_SCREEN)) {
            ((ChooseAccountingController) screensFramework.getScreensController().getController(ClientScreensFramework.THIRD_SCREEN)).loadData(client);
        }
    }

    private void chooseNextPage() {
        String actual = screensFramework.getScreensController().getActual();
        if (actual.equals(ClientScreensFramework.FIRST_SCREEN)) {
            if (screensFramework.getScreensController().getController(ClientScreensFramework.FIRST_SCREEN).validateForm()) {
                this.client = ((ChooseClientController) screensFramework.getScreensController().getController(ClientScreensFramework.FIRST_SCREEN)).getClient();
                this.startDate = ((ChooseClientController) screensFramework.getScreensController().getController(ClientScreensFramework.FIRST_SCREEN)).getStartDate();
                this.endDate = ((ChooseClientController) screensFramework.getScreensController().getController(ClientScreensFramework.FIRST_SCREEN)).getEndDate();
                screensFramework.getScreensController().setScreen(ClientScreensFramework.SECOND_SCREEN);
                step1.setTextFill(Color.BLACK);
                step2.setTextFill(Color.GREEN);
                prev.setVisible(true);
            }
        }
        if (actual.equals(ClientScreensFramework.SECOND_SCREEN)) {
            if (screensFramework.getScreensController().getController(ClientScreensFramework.SECOND_SCREEN).validateForm()) {
                this.car = ((ChooseCarController) screensFramework.getScreensController().getController(ClientScreensFramework.SECOND_SCREEN)).getCar();
                screensFramework.getScreensController().setScreen(ClientScreensFramework.THIRD_SCREEN);
                step2.setTextFill(Color.BLACK);
                step3.setTextFill(Color.GREEN);
                useDefault.setVisible(true);
            }
        }
        if (actual.equals(ClientScreensFramework.THIRD_SCREEN)) {
            if (screensFramework.getScreensController().getController(ClientScreensFramework.THIRD_SCREEN).validateForm()) {
                this.accounting = ((ChooseAccountingController) screensFramework.getScreensController().getController(ClientScreensFramework.THIRD_SCREEN)).getAccountingData();
                screensFramework.getScreensController().setScreen(ClientScreensFramework.FOUR_SCREEN);
                useDefault.setVisible(false);
                find.setVisible(false);
                step3.setTextFill(Color.BLACK);
                step4.setTextFill(Color.GREEN);
            }
        }
        if (actual.equals(ClientScreensFramework.FOUR_SCREEN)) {
            if (screensFramework.getScreensController().getController(ClientScreensFramework.FOUR_SCREEN).validateForm()) {
                this.tarrif = ((ChooseTarrifController) screensFramework.getScreensController().getController(ClientScreensFramework.FOUR_SCREEN)).getTarrifId();
                screensFramework.getScreensController().setScreen(ClientScreensFramework.FIVE_SCREEN);
                step4.setTextFill(Color.BLACK);
                step5.setTextFill(Color.GREEN);
            }
        }
        if (actual.equals(ClientScreensFramework.FIVE_SCREEN)) {
            if (screensFramework.getScreensController().getController(ClientScreensFramework.FIVE_SCREEN).validateForm()) {
                this.space = ((ChooseParkingSpaceController) screensFramework.getScreensController().getController(ClientScreensFramework.FIVE_SCREEN)).getChoosedSpace();
                screensFramework.getScreensController().setScreen(ClientScreensFramework.SIX_SCREEN);
                next.setText("Potwierdź");
                step5.setTextFill(Color.BLACK);
                step6.setTextFill(Color.GREEN);
            }
        }
        if (actual.equals(ClientScreensFramework.SIX_SCREEN)) {
            if (isConfirmed) {
                isConfirmed = false;
                screensFramework.getScreensController().setScreen(ClientScreensFramework.FIRST_SCREEN);
                myController.setScreen(ScreensFramework.MAIN_SCREEN);
                prev.setText("Wstecz");
                prev.setVisible(false);
                next.setText("Dalej");
                clearAllData();
            } else {
                isConfirmed = true;
                prev.setText("Drukuj");
                next.setText("Zakończ");
                if (fetchDataToDatabase()) {
                    ((EndPageController) screensFramework.getScreensController().getController(ClientScreensFramework.SIX_SCREEN)).showBarCode(passDB.getParkingPassId());
                    createPopup("Dodano nową przepustkę");
                } else {
                    createPopup("Wystąpił krytyczny błąd");

                }

            }
        }
    }

    private boolean fetchDataToDatabase() {
        try {
            ClientsModel clientDB;
            CarsModel carDB;
            AccountingdataModel accDB;
            ReceivablesModel recDB;
            if (this.selectedClient != null) {
                this.client.setClientId(selectedClient);
                clientDB = DaoInstanceEnum.INSTANCE.getDao().updateClient(client);
            } else {
                clientDB = DaoInstanceEnum.INSTANCE.getDao().addClient(client);
            }
            if (this.selectedCar != null) {
                this.car.setCarId(selectedCar);
                carDB = DaoInstanceEnum.INSTANCE.getDao().updateCar(car);
            } else {
                carDB = DaoInstanceEnum.INSTANCE.getDao().addCar(car);
            }
            accDB = DaoInstanceEnum.INSTANCE.getDao().addAccountingdata(accounting);
            ParkingpassesModel pass = new ParkingpassesModel(startDate, startDate, "Temporary");
            pass.setCarId(carDB);
            pass.setNumberId(space);
            pass.setTarrif(tarrif);
            passDB = DaoInstanceEnum.INSTANCE.getDao().addPass(pass);
            PassCost passCost = new PassCost();
            Double cost = passCost.countCostOfPass(startDate, endDate, tarrif);
            ReceivablesModel rec = new ReceivablesModel(cost, startDate, endDate);
            rec.setParkingPassId(pass);
            recDB = DaoInstanceEnum.INSTANCE.getDao().addReceivable(rec);
            PassesrecordsModel records = new PassesrecordsModel(new Date());
            records.setClientId(clientDB);
            records.setAccountingDataId(accDB);
            records.setParkingPassId(passDB);
            DaoInstanceEnum.INSTANCE.getDao().addPassRecord(records);

            return true;
        } catch (Throwable t) {
            t.printStackTrace();
            return false;
        }
    }

    private void clearAllData() {
        ((ChooseClientController) screensFramework.getScreensController().getController(ClientScreensFramework.FIRST_SCREEN)).clearData();
        ((ChooseCarController) screensFramework.getScreensController().getController(ClientScreensFramework.SECOND_SCREEN)).clearData();
        ((ChooseAccountingController) screensFramework.getScreensController().getController(ClientScreensFramework.THIRD_SCREEN)).clearData();
        ((EndPageController) screensFramework.getScreensController().getController(ClientScreensFramework.SIX_SCREEN)).hashCode();
        find.setVisible(true);
        find.setText("Szukaj");
        step1.setTextFill(Color.GREEN);
        step6.setTextFill(Color.BLACK);
        this.accounting = null;
        this.car = null;
        this.client = null;
        this.endDate = null;
        this.isConfirmed = false;
        this.passDB = null;
        this.space = null;
        this.selectedCar = null;
        this.selectedClient = null;
        this.tarrif = null;
    }

    private void createPopup(String text) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("popup.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Potwierdzenie");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            PopupController controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setPopupText(text);
            dialogStage.showAndWait();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void choosePrevPage() {
        String actual = screensFramework.getScreensController().getActual();
        if (actual.equals(ClientScreensFramework.FIRST_SCREEN)) {
        }
        if (actual.equals(ClientScreensFramework.SECOND_SCREEN)) {
            screensFramework.getScreensController().setScreen(ClientScreensFramework.FIRST_SCREEN);
            prev.setVisible(false);
            step1.setTextFill(Color.GREEN);
            step2.setTextFill(Color.BLACK);
        }
        if (actual.equals(ClientScreensFramework.THIRD_SCREEN)) {
            screensFramework.getScreensController().setScreen(ClientScreensFramework.SECOND_SCREEN);
            useDefault.setVisible(false);
            step2.setTextFill(Color.GREEN);
            step3.setTextFill(Color.BLACK);
        }
        if (actual.equals(ClientScreensFramework.FOUR_SCREEN)) {
            screensFramework.getScreensController().setScreen(ClientScreensFramework.THIRD_SCREEN);
            useDefault.setVisible(true);
            find.setVisible(true);
            step3.setTextFill(Color.GREEN);
            step4.setTextFill(Color.BLACK);
        }
        if (actual.equals(ClientScreensFramework.FIVE_SCREEN)) {
            screensFramework.getScreensController().setScreen(ClientScreensFramework.FOUR_SCREEN);
            step4.setTextFill(Color.GREEN);
            step5.setTextFill(Color.BLACK);
        }
        if (actual.equals(ClientScreensFramework.SIX_SCREEN)) {
            if (isConfirmed) {
                ((EndPageController) screensFramework.getScreensController().getController(ClientScreensFramework.SIX_SCREEN)).printBarCode();
            } else {
                screensFramework.getScreensController().setScreen(ClientScreensFramework.FIVE_SCREEN);
                next.setText("Dalej");
                step5.setTextFill(Color.GREEN);
                step6.setTextFill(Color.BLACK);
            }
        }

    }

    public void setSelectedClient(Integer id) {
        selectedClient = id;
    }

    public void setSelectedCar(Integer id) {
        selectedCar = id;
    }
}
